using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Framework.Library;
using Framework.Library.Components.Modules;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Tabs;
using Framework.Library.Utilities;
using Framework.Module.Logging.Statistic;

namespace Portal.Home
{
    public partial class Default : BasePage
	{
        private int tabId;
        private int portalId;
        private string title = ConvertUtility.ToString(PortalSetting.GetSetting()[Definition.Portal_SiteName + "_" + AppEnv.PortalId() + "_" + AppEnv.GetLanguage()]);
        public string style = "";
        public string url = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            #region loging detail
            if (Request.QueryString["catid"] != null)
            { 
                Loging1.CounterKey = "chuyenmuc"; 
                Loging1.CounterID = Request.QueryString["catid"];
            }
            else
                Loging1.CounterKey = "homepage";
            #endregion

            #region loging summary
            int hournow = DateTime.Now.Hour;
            int hour = 0;
            StaticAccessOnlineInfo statisticInfo = new StaticAccessOnlineInfo();
            if (Application["Hour"] != null)
            {
                hour = (int)Application["Hour"];

                if (hour == hournow)
                {
                    string dateaccess = hour.ToString();
                    statisticInfo.DateAccess = dateaccess;
                    statisticInfo.DateUpdate = DateTime.Now;
                    statisticInfo.WeekAccess = Convert.ToInt32(DateTime.Now.DayOfWeek);
                    statisticInfo.MonthAccess = DateTime.Now.Month;
                    StatisticAccessOnlineDB.Update(statisticInfo);
                }
                else
                {
                    string dateaccess = hournow.ToString();
                    statisticInfo.DateAccess = dateaccess;
                    statisticInfo.DateUpdate = DateTime.Now;
                    statisticInfo.WeekAccess = Convert.ToInt32(DateTime.Now.DayOfWeek);
                    statisticInfo.MonthAccess = DateTime.Now.Month;
                    StatisticAccessOnlineDB.Insert(statisticInfo);
                }
            }
            else
            {
                string dateaccess = hournow.ToString();
                statisticInfo.DateAccess = dateaccess;
                statisticInfo.DateUpdate = DateTime.Now;
                statisticInfo.WeekAccess = Convert.ToInt32(DateTime.Now.DayOfWeek);
                statisticInfo.MonthAccess = DateTime.Now.Month;
                StatisticAccessOnlineDB.Insert(statisticInfo);
            }            
            Application["Hour"] = hournow;
            #endregion

            PortalSetting setting = PortalSetting.GetSetting();            
            tabId = ConvertUtility.ToInt32(Request.QueryString["tabid"]);
            portalId = ConvertUtility.ToInt32(AppEnv.PortalId());
      
            string sl = ConvertUtility.ToString(setting[Definition.Portal_Style + "_" + portalId + "_" + AppEnv.GetLanguage()]);            
            style = "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + sl + "/Default.css" + "\" />";

            if (tabId == 0)
            {
                tabId = ConvertUtility.ToInt32(setting[Definition.Portal_Home_Tab + "_" + portalId + "_" + AppEnv.GetLanguage()]);
            }
            
            TabInfo tab = TabController.GetTab(tabId);
            if (tab == null)
            {
                return;
            }

            GetPath(tab.TabID);
            titTitle.InnerHtml = title;
            if (tab.TabLayout != null && tab.TabLayout != string.Empty)
            {
                if (tab.TabUrl != string.Empty)
                {
                    Response.Redirect(tab.TabUrl);
                }
                else
                {
                    PlaceHolder1.Controls.Add((UserControl)LoadControl(tab.TabLayout));
                }
            }
        }
        
        private void GetPath(int currentTabId)
        {
            ArrayList path = new ArrayList();
            TabInfo curTab = TabController.GetTab(currentTabId);
            while (curTab != null)
            {
                path.Add(curTab);
                curTab = TabController.GetTab(curTab.TabParentID);
            }
            path.Reverse();           
        }
	}
}
